package my.springboot3.controller;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import my.springboot3.dto.PageDto;
import my.springboot3.dto.Respond;
import my.springboot3.dto.entity.Article;
import my.springboot3.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.nio.file.Path;
import java.nio.file.Paths;

@RestController
@Controller
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    /**
     * 分页查询文章
     */
    @GetMapping
    public Respond<PageDto<Article>> article(@RequestParam Integer pageNum,
                                            @RequestParam Integer pageSize,
                                            @RequestParam(required = false) Integer categoryId,
                                            @RequestParam(required = false) String state) {
        // 分页查询文章
        PageDto<Article> pageDto = articleService.ListAll(pageNum, pageSize, categoryId, state);
        return Respond.success(pageDto);
    }

    @PostMapping
    public Respond<Void> add(@RequestBody @Valid Article article) {
        // 添加文章
        articleService.add(article);
        return Respond.success();
    }


    /**
     * 删除文章
     */
    @DeleteMapping
    public Respond<Void> delete(@RequestParam Integer id) {
        // 删除文章
        articleService.deleteById(id);
        return Respond.success();
    }





}